//  On my honor:
//	
//  - I have not used source code obtained from another student, 
//    or any other unauthorized source, either modified or unmodified.  
//	
//  - All source code and documentation used in my program 
//    is either my original work, or was derived by me from the source
//    code published in the textbook for this course.
//	
//  - I have not discussed coding details about this project with anyone
//    other than my instructor, ACM/UPE tutors or the GTAs assigned to this
//    course. I understand that I may discuss the concepts of this program
//    with other students, and that another student may help me debug my
//    program so long as neither of us writes anything during the discussion
//    or modifies any computer file during the discussion.  I have violated
//    neither the spirit nor letter of this restriction.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class PRprog {

	/**
	 * @param args
	 *            command-line arguments
	 */
	public static void main(String[] args) {
		// check argument count
		if (args.length != 1) {
			System.err.println("Run PRprog with the proper arguments.");
			System.err
					.println("\tPRprog <command-file>");
			System.err.println("command-file\ttext file with commands");
		} else {
			if (!parseArguments(args)) {
				System.err.println("The program encountered an error while");
				System.err.println("attempting to process the file.");
			}
		}
	}

	/**
	 * parse the arguments passed to the program
	 */
	public static boolean parseArguments(String[] args) {
		MemManager manager;
		String line = "";
		String fileName = args[0];

		manager = new MemManager();
		// attempt to open the file
		try {
			// open the file with name fileName
			File inFile = new File(fileName);
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(inFile)));

			// iterate through file line by line
			while ((line = br.readLine()) != null) {
				StringTokenizer tokenizer = new StringTokenizer(line);

				// parse the lines by string tokens and process commands
				if (tokenizer.hasMoreTokens()) {
					String command = tokenizer.nextToken();

					// takes "insert x y city_name" with any
					// whitespace format
					if (command.equals("insert")) {
						// attempt to parse x and y values from file
						int x, y;
						try {
							x = (Integer.parseInt(tokenizer.nextToken()));
							y = (Integer.parseInt(tokenizer.nextToken()));
						} catch (NumberFormatException exc) {
							System.err
									.println("insert command called with x, y");
							System.err.println("coordinates in wrong format.");
							return false;
						}
						String cityName = tokenizer.nextToken();

						manager.insert(x, y, cityName);
					}

					// takes "remove x y" or "remove cityName" with any whitespace format
					else if (command.equals("remove")) {
						if (tokenizer.countTokens() == 2) {
							// attempt to parse the coordinates of city to be removed
							int x, y = 0;
							try {
								x = (Integer.parseInt(tokenizer.nextToken()));
								y = (Integer.parseInt(tokenizer.nextToken()));
							} catch (NumberFormatException exc) {
								System.err
										.println("remove command called with x, y");
								System.err.println("coordinates in wrong format.");
								return false;
							}
							manager.remove(x, y);
						}
						else if(tokenizer.countTokens() == 1)
						{
							String cityName = tokenizer.nextToken();
							manager.remove(cityName);
						}
						else
						{
							System.err.println("remove command called with wrong format");
							return false;
						}
					}
					
					// takes "find cityName" command with any whitespace format
					else if (command.equals("find"))
					{
						if(tokenizer.countTokens() == 1)
						{
							String cityName = tokenizer.nextToken();
							manager.find(cityName);
						}
						else
						{
							System.err.println("find command called with wrong format");
							return false;
						}
					}
					else if(command.equals("search"))
					{
						if(tokenizer.countTokens() == 3)
						{
							// attempt to parse the coordinates and radius to be searched
							int x, y, radius = 0;
							try {
								x = (Integer.parseInt(tokenizer.nextToken()));
								y = (Integer.parseInt(tokenizer.nextToken()));
								radius = (Integer.parseInt(tokenizer.nextToken()));
							} catch (NumberFormatException exc) {
								System.err
										.println("remove command called with x, y, radius");
								System.err.println("coordinates in wrong format.");
								return false;
							}
							manager.search(x, y, radius);
						}
						else
						{
							System.err.println("search command called with wrong format");
							return false;
						}
					}
					else if(command.equals("makenull"))
					{
						manager.makenull();
					}
					else if(command.equals("debug"))
					{
						manager.debug();
					}
					else {
						System.err
								.println("Unknown command or wrong file format.");
						System.err.println("Please check the " + fileName
								+ " file");
						return false;
					}
				}
			}
			br.close();
		} catch (FileNotFoundException ex) {
			System.err.println("File does not exist!");
			return false;
		} catch (IOException ex) {
			System.err.println("IOException error");
			return false;
		}
		return true;
	}

}
